#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <assert.h>
#include <dirent.h>
#include <sys/stat.h>

#include <rpm/rpmlib.h>

#include "rapt-compat.h"
#include "crpmtag.h"

int main(int c, char **v) {
    const char *progname = v[0];
    if (c != 3)
	return 1;

    char *orig_pkglist = v[1];
    char *stripped_pkglist = v[2];

    FD_t Fd = Fopen(orig_pkglist, "r.ufdio");
    if (Ferror(Fd)) {
	fprintf(stderr, "%s: %s: %s\n", progname, orig_pkglist, Fstrerror(Fd));
	return 1;
    }

    FD_t outfd = Fopen(stripped_pkglist, "w+");
    if (!outfd) {
	fprintf(stderr, "%s: error creating file %s: %s\n", progname, stripped_pkglist, Fstrerror(Fd));
	return 1;
    }

    Header h;
    while ((h = headerRead(Fd, HEADER_MAGIC_YES)) != NULL) {
	headerDel(h, CRPMTAG_FILESIZE);
	headerDel(h, CRPMTAG_MD5);
	Header newHeader = headerCopy(h);
	headerWrite(outfd, newHeader, HEADER_MAGIC_YES);
	headerFree(newHeader);
	headerFree(h);
    }
    Fclose(Fd);
    Fclose(outfd);

    return 0;
}
